#include <iostream>
#include "userservice_impl.h"
#include "book.h"
#include "reader.h"
#include "tools.h"
#include <vector>
#include "userdao.h"
#include "userdao_impl.h"
#include<iomanip>
#include<getch.h>
#include <algorithm>
#include <map>

using namespace std;
vector<Book> books;//书籍容器
vector<Reader> readers;//读者容器
vector<Remark> remarks;//评语容器

UserServiceImpl::UserServiceImpl()
{
    r_dao = new UserDaoImpl;//创建用户数据存取对象
    b_dao = new BookDaoImpl;//创建书籍存取对象
    re_dao = new RemarkDaoImpl;//创建用户数据存取对象
}

//读者搜索书籍
void UserServiceImpl::ReaderSearch(int id)
{
	r_dao->load(readers);
	unsigned int i=0;
	for(;i<readers.size();i++)
	{
	    if(readers[i].get_id() == id)
	    {
	        readers[i].show();
			//在读书籍
		    for(unsigned int j=0;j<readers[i].books.size();j++)
		    {
		    	cout << "\e[34m第\e[0m" << j+1 << "\e[34m本书，书编号:\e[0m";
		    	cout << readers[i].books[j].GetId() << "    ";
		    	cout << "\e[34m书的名字:\e[0m" <<readers[i].books[j].GetName() << endl;
		    }
			cout << "\e[32m====================================================\e[0m" << endl;
			
			//历史书籍
			cout << "\e[34m历史书籍列表：\e[0m" << endl;
			unsigned int j=0;
			for(;j<readers[i].bookid.size();j++)
			{
				cout << "\e[34m第\e[0m" << j+1 << "\e[34m本书，书编号:\e[0m";
				cout << readers[i].bookid[j] << endl;
			}
			cout << "\e[32m****************************************************\e[0m" << endl;
			cout << endl;
			if(0 == readers[i].bookid.size())
			{
				break;
			}
			cout << "\e[36m      请给你心爱的书籍来条评语吧！\e[0m" << endl;
		    cout << "\e[36m        q、忍心退出\e[0m" << endl;
			getch();
			int cmd;
			cmd = getch();
		    if('q' != cmd)
		    {
		        RemarkRun(i);
		    }
			break;	
    	}
	}
	if(i >= readers.size()) 
	{
		cout << "\e[31m查无此人\e[0m" << endl;
	}
	r_dao->save(readers);
}

//读者对历史书籍进行评价
void UserServiceImpl::RemarkRun(int i)
{
    while(1)
	{
		re_dao->load(remarks);
		system("clear");
		//历史书籍
		cout << "\e[34m历史书籍列表：\e[0m" << endl;
		unsigned int j=0;
		for(;j<readers[i].bookid.size();j++)
		{
			cout << "\e[32m*******第\e[0m" << j+1 << "\e[32m本书，书编号:\e[0m";
			cout << readers[i].bookid[j] << endl;
		}
		cout <<  "\e[34m9.退出\e[0m" << endl;
		cout << "\e[34m请输入是第几本书：\e[0m";
		char cmd = 0;
		char max_book = readers[i].bookid.size() +'0';
		while(1)
		{
			cmd = getch();
			if(cmd == '9') return;
			if(cmd >='1' && cmd <= max_book)
				break;
		}
		int num = cmd - '0';
		int reader_id = readers[i].get_id();
		int book_id = readers[i].bookid[num-1];
		char buf[255];
		cout << cmd << endl;
		cout << "\e[34m请输入评语：\e[0m";
		cin >> buf;
		Remark* remark = new Remark(book_id,reader_id,buf);
		//容器
		remarks.push_back(*remark);
		cout << "  \e[35m书评：\e[0m" << remarks[0].GetBuf() << endl;
		//cout << remarks.size();
		re_dao->save(remarks);
 	}
}

//对书籍进行查找
void UserServiceImpl::BookSearch(char *str)
{
	books.clear();
	remarks.clear();
	re_dao->load(remarks);
	b_dao->load(books);
	for(unsigned int i=0;i<books.size();i++)
	{
		if(check_book(books.at(i).GetName(),str))
		{
		books[i].AllShow();
		for(unsigned int j=0;j<remarks.size();j++)
		{
		//	cout << remarks[j].GetBookId() << endl;
			if(remarks[j].GetBookId() == books[i].GetId())
			{
				cout << remarks[j].GetReaderId() << ":";
				cout << remarks[j].GetBuf() << endl;
			}
		}
		}
	}
}
//读者借书
bool UserServiceImpl::BookOrder(int r_id,int b_id)
{
    
    b_dao->load(books);
	for(unsigned int i=0;i<books.size();i++)
	{
	    if(b_id == books[i].GetId())
		{
		    if(books[i].GetBorrow()) 
		    {
		        if(books[i].GetAppoint() == 0)
		        {
		            books[i].SetAppoint(r_id);
		            b_dao->save(books);
		            return true;
		        }
		    }
		}
	} 
	return false;
}
void UserServiceImpl::Recommend()
{
    while(1)
	{
		system("clear");
		cout << "\e[33m*************推荐***************\e[0m" << endl;
		cout << "\e[33m        1、按借阅次数排名推荐\e[0m" << endl;
		cout << "\e[33m        2、按评分排名推荐\e[0m" << endl;
		cout << "\e[33m        3、退出\e[0m" << endl;
		cout << "\e[33m*********************************\e[0m" << endl;
		char cmd = 0;
		while(1)
		{
			cmd = getch();
			if(cmd >='1' && cmd <='3')
				break;
		}
		cout << cmd << endl;
		switch(cmd-'0')
		{
			case 1:Cnt();break;
			case 2:Soc();break;
			case 3:return;
			
		}
 	}
}

bool UserServiceImpl::Compare1(Book b1,Book b2)
{
    if(b1.GetCnt() > b2.GetCnt())
    {
        return true;
    }
    else
    {
        return false;
    }
}
bool UserServiceImpl::Compare2(Book b1,Book b2)
{
    if(b1.GetScore() > b2.GetScore())
    {
        return true;
    }
    else
    {
        return false;
    }
}

//按照借阅次数来向读者推荐
void UserServiceImpl::Cnt()
{
    books.clear();
	b_dao->load(books);
	sort(books.begin(),books.end(),UserServiceImpl::Compare1);
	map<string,int>m;
    cout << "\e[32m借阅次数前三图书\e[0m" << endl;
	unsigned int i = 0;
	int count = 0;
	for(i = 0;i < books.size();i++)
	{
		if(3 <= count)
		{
			break;
		}
        if(0 != m[books[i].m_name])
		{
			continue;
		}
		++m[books[i].m_name];
		
		cout << "\e[35m书名：\e[0m" << books[i].m_name << "\e[35m  编号：\e[0m" << books[i].m_id << "\e[35m  借阅次数：\e[0m" << books[i].GetCnt() << endl;
			
		++count;
	}
	anykey_continue();
}

//按评分来向读者推荐
void UserServiceImpl::Soc()
{
    books.clear();
	b_dao->load(books);
	sort(books.begin(),books.end(),UserServiceImpl::Compare2);
	map<string,int>m;
    cout << "\e[32m评分前三图书\e[0m" << endl;
	unsigned int i = 0;
	int count = 0;
	for(i = 0;i < books.size();i++)
	{
		if(3 <= count)
		{
			break;
		}
        if(0 != m[books[i].m_name])
		{
			continue;
		}
		++m[books[i].m_name];
		
		cout << "\e[35m书名：\e[0m" << books[i].m_name << "\e[35m  编号：\e[0m" << books[i].m_id << "\e[35m  评分：\e[0m" << books[i].GetScore() << endl;
			
		++count;
	}
	anykey_continue();
}
UserServiceImpl::~UserServiceImpl()
{
	delete r_dao;
	delete b_dao;
}
